/** 
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
* 
* PmBudgetApplyServiceAuto
* 项目预算申请单
* 
* 修改纪录
* 2020-05-22 版本：1.0 shenyouyi 创建。
* @version 版本：1.0
* @author 作者：shenyouyi
* 创建日期：2020-05-22
*/

package com.topscomm.pm.service.impl.auto;

import javax.annotation.Resource;
import com.topscomm.basic.IBasicDao;
import com.topscomm.tap.common.DatacodeCache;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import com.topscomm.pm.*;
import com.topscomm.pm.pojo.PmBudgetApplyEntity;
import com.topscomm.pm.pojo.PmBudgetLineEntity;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.IPmBudgetLineService;
import com.topscomm.pm.service.IPmProjectService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pm.dao.IPmBudgetApplyDao;


public class PmBudgetApplyServiceAuto extends PmService<PmBudgetApplyEntity,IPmBudgetApplyDao> {
	
	/**
	 * @author: shenyouyi
	 * @date: 2020-05-22
	 * @param mapRow
	 * @description:设置字段显示值
	 * @modify:
	 */
	@Override
	public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) {
		String temp;
		if (mapRow.get("projectid") != null) {
			temp = mapRow.get("projectid").toString();
			mapRow.put("dispprojectid", datacodeCache.getValueDynamic("PmProject", temp, "id", "name"));
		}
		if (mapRow.get("flowid") != null) {
			temp = mapRow.get("flowid").toString();
			mapRow.put("dispflowid", datacodeCache.getValueDynamic("ApprovalFlow_PmBudgetApply", temp, "id", "name"));
		}
		if (mapRow.get("state") != null) {
			temp = mapRow.get("state").toString();
			mapRow.put("dispstate", datacodeCache.getValueDynamic("TapApprovalBillState", temp, "code", "name"));
		}
		if (mapRow.get("enabled") != null) {
			temp = mapRow.get("enabled").toString();
			mapRow.put("dispenabled", datacodeCache.getValueDynamic("CboYesNo", temp, "code", "name"));
		}
		// 
		// 获取当前项目下所有的预算（包括审核未通过的）总金额==============================开始====================
		IPmBudgetLineService pmBudgetLineService= (IPmBudgetLineService)SpringUtil.getBean("pmBudgetLineService");
		StringBufferProxy bugetLineListAllSql = new StringBufferProxy();
		bugetLineListAllSql.appendSingle("projectid = '{0}'",ConvertUtil.convertToString(mapRow.get("projectid")));
		List<PmBudgetLineEntity> bugetLineListAll = pmBudgetLineService.queryByWhere(bugetLineListAllSql.toString());
		BigDecimal totalBudget = new BigDecimal("0.0000");
		for(PmBudgetLineEntity item:bugetLineListAll) {
			BigDecimal tempBugdgetMoney = new BigDecimal(item.getBudgetmoney());
			totalBudget=totalBudget.add(tempBugdgetMoney);
		}
		// 获取当前项目下所有的预算（包括审核未通过的）总金额==============================结束====================
		// 当前毛利率，获取当前项目下已通过审核的预算总金额==============================开始====================
		StringBufferProxy bugetLineListCureentSql = new StringBufferProxy();

		bugetLineListCureentSql.appendSingle("projectid = '{0}'  and state=9",ConvertUtil.convertToString(mapRow.get("projectid")));

		List<PmBudgetLineEntity> bugetLineListCureent = pmBudgetLineService.queryByWhere(bugetLineListCureentSql.toString());
		BigDecimal totalBudgetCurrent = new BigDecimal("0.0000");
		for(PmBudgetLineEntity item:bugetLineListCureent) {
			BigDecimal tempBugdgetMoney = new BigDecimal(item.getBudgetmoney());
			totalBudgetCurrent=totalBudgetCurrent.add(tempBugdgetMoney);
		}
		DecimalFormat df = new DecimalFormat("0.00%");

		// 当前毛利率，获取当前项目下已通过审核的预算总金额==============================结束====================
		IPmProjectService pmProjectService= (IPmProjectService)SpringUtil.getBean("pmProjectService");
		PmProjectEntity projectEntity = pmProjectService.queryById(ConvertUtil.convertToString(mapRow.get("projectid")));
		BigDecimal projectMoney = projectEntity.getProjectmoney();// 项目金额
		mapRow.put("estimateProfitRate", (projectMoney.subtract(totalBudget)).divide(projectMoney,4).setScale(4));// 预计毛利率
		mapRow.put("estimateProfitRateForMobile", df.format((projectMoney.subtract(totalBudget)).divide(projectMoney,4).setScale(4)));// 预计毛利率
		mapRow.put("currentProfitRate", projectMoney.subtract(totalBudgetCurrent).divide(projectMoney,4).setScale(4));// 预计毛利率
		mapRow.put("currentProfitRateMobile", df.format(projectMoney.subtract(totalBudgetCurrent).divide(projectMoney,4).setScale(4)));// 预计毛利率
	}

	@Override
	@Resource(name = "pmBudgetApplyDao")
	public void setDao(IBasicDao<PmBudgetApplyEntity> dao) {		
		super.setDao(dao);
	}
}
